package com.naizhuai.ui.task.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.google.gson.Gson;
import com.naizhuai.R;
import com.naizhuai.ui.task.TaskReleaseActivity;

import java.util.List;

/**
 * @author：lichenxi
 * @date 2018/4/2 17
 * email：525603977@qq.com
 * Fighting
 */
public class TaskExplainAdapter extends RecyclerView.Adapter<TaskExplainAdapter.ViewHolder> {

    private List<String> mStringList;
   private boolean editEnable=true;

    public void setEditEnable(boolean editEnable) {
        this.editEnable = editEnable;
    }

    public void setStringList(List<String> stringList) {
        mStringList = stringList;
        notifyDataSetChanged();
    }


    public TaskExplainAdapter() {
    }

    public TaskExplainAdapter(List<String> stringList) {
        mStringList = stringList;
    }

    public String getTaskString(){
//      StringBuilder stringBuilder=new StringBuilder();
//      for (String str:mStringList){
//          if (!TextUtils.isEmpty(str)){
//              stringBuilder.append(str);
//              stringBuilder.append(",");
//          }
//      }
//      if (stringBuilder.length()>0)
//           stringBuilder.deleteCharAt(stringBuilder.length()-1);
//        return stringBuilder.toString();
        //要保证至少一个不是空
        for (String str:mStringList){
            if (!TextUtils.isEmpty(str)){
                return new Gson().toJson(mStringList);
            }
        }
      return "";
    }
    @NonNull

    @Override
    public TaskExplainAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.task_item_explain, parent, false);
        return new ViewHolder(root,new MyCustomEditTextListener());
    }

    @Override
    public void onBindViewHolder(@NonNull TaskExplainAdapter.ViewHolder holder, int position) {
        holder.mEditTextListener.updatePosition(holder.getAdapterPosition());
        holder.textDescribe.setText(mStringList.get(holder.getAdapterPosition()));
        holder.textDescribe.setFocusable(editEnable);
    }

    @Override
    public int getItemCount() {
        return mStringList==null?0:mStringList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        EditText textDescribe;
        private MyCustomEditTextListener mEditTextListener;
        public ViewHolder(View itemView,MyCustomEditTextListener mEditTextListener) {
            super(itemView);
            this.mEditTextListener=mEditTextListener;
            textDescribe = itemView.findViewById(R.id.textDescribe);
            textDescribe.addTextChangedListener(mEditTextListener);
        }
    }
    private class MyCustomEditTextListener implements TextWatcher {
        private int position;

        public void updatePosition(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            // no op
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            mStringList.set(position,charSequence.toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {
            // no op
        }
    }
}
